import dotenv
from langchain import hub
from langchain.agents import create_react_agent, AgentExecutor
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_core.tools import render_text_description_and_args
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
from langchain_community.tools import GoogleSerperRun

dotenv.load_dotenv()


class GoogleSerperSchema(BaseModel):
    query: str = Field(description="执行谷歌搜索的查询语句")


google_serper = GoogleSerperRun(
    name="google_serper",
    description=(
        "一个低成本的谷歌搜索工具"
        "当你想要回答有关问题的时候，可以调用该工具"
        "该工具的输入是搜索查询语句"
    ),
    api_wrapper=GoogleSerperAPIWrapper()
)

tools = [google_serper]

# 定义智能体提示模板
prompt = hub.pull("hwchase17/react")

# 创建大语言模型
llm = ChatOpenAI(model="gpt-4o-mini")
agent = create_react_agent(
    llm=llm,
    tools=tools,
    prompt=prompt,
    tools_renderer=render_text_description_and_args
)

agent_executer = AgentExecutor(agent=agent,tools=tools,verbose=True)
print(agent_executer.invoke({"input":"最近五年A股的价格"}))
# print(agent_executer.invoke({"input":"你好，你是？"})) 问题没有推理，不使用工具，会报错